package com.yyzw.activiti.modeler.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyzw.activiti.modeler.dao.SysBusinessExtendMapper;
import com.yyzw.activiti.modeler.dao.SysBusinessMapper;
import com.yyzw.activiti.modeler.model.*;
import com.yyzw.activiti.modeler.service.SysBusinessService;
import net.sf.jasperreports.engine.JREmptyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysBusinessServiceImpl implements SysBusinessService {

    @Autowired
    SysBusinessMapper sysBusinessMapper;

    @Autowired
    SysBusinessExtendMapper sysBusinessExtendMapper;

    @Override
    public ResponseVo deleteBusReportBusId(int busId) {
        sysBusinessMapper.deleteBusinessByBusId(busId);
        sysBusinessExtendMapper.deleteBusinessByBusid(busId);
        return new ResponseVo(true,"删除成功");
    }

    @Override
    public DataGridRespVo queryBusExtendByBusId(int busId,int page,int pageSize) {
        Example example = new Example(SysBusinessExtend.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busId",busId);
        List<SysBusinessExtend> list = sysBusinessExtendMapper.selectByExample(example);
        PageHelper.startPage(page,pageSize);
        PageInfo<SysBusinessExtend> pageInfo = new PageInfo<>(list);
        DataGridRespVo dataGridRespVo = new DataGridRespVo();
        dataGridRespVo.setRows(list);
        dataGridRespVo.setTotal(pageInfo.getTotal());
        return dataGridRespVo;
    }

    @Override
    public ResponseVo getReportById(int busId) {
        SysBusiness sysBusiness =new SysBusiness();
        sysBusiness.setBusId(busId);
        SysBusiness business = this.sysBusinessMapper.selectByPrimaryKey(sysBusiness);
        if(business!=null){
            return new ResponseVo(true,"查询成功",business);
        }
        return new ResponseVo(false,"查询失败");
    }

    @Override
    public ResponseVo updateBusReport( SysBusinessVo sysBusinessVo) {
        SysBusiness sysBusiness = sysBusinessVo.getSysBusiness();
        sysBusiness.setBusTime(new Date());
        this.sysBusinessMapper.updateByPrimaryKey(sysBusiness);
        SysBusinessExtend[] sysBusinessExtendUpdate = sysBusinessVo.getSysBusinessExtendUpdate();
        if(sysBusinessExtendUpdate!=null){
            for(SysBusinessExtend sysBusinessExtend1:sysBusinessExtendUpdate){
                sysBusinessExtend1.setUpdateTime(new Date());
                this.sysBusinessExtendMapper.updateByPrimaryKey(sysBusinessExtend1);
            }
        }
        SysBusinessExtend[] sysBusinessExtendDelete = sysBusinessVo.getSysBusinessExtendDelete();
        if(sysBusinessExtendDelete!=null){
            for(SysBusinessExtend sysBusinessExtend1:sysBusinessExtendDelete){
                this.sysBusinessExtendMapper.deleteByPrimaryKey(sysBusinessExtend1);
            }
        }
        return new ResponseVo(true,"修改成功");
    }

    @Override
    public ResponseVo addBusReport(SysBusinessVo sysBusinessVo) {
        SysBusiness sysBusiness = sysBusinessVo.getSysBusiness();
        sysBusiness.setBusTime(new Date());
        this.sysBusinessMapper.insert(sysBusiness);
        SysBusinessExtend[] sysBusinessExtend = sysBusinessVo.getSysBusinessExtend();
        for(SysBusinessExtend sysBusinessExtend1:sysBusinessExtend){
            sysBusinessExtend1.setBusId(sysBusiness.getBusId());
            sysBusinessExtend1.setCreateTime(new Date());
            sysBusinessExtend1.setUpdateTime(new Date());
            this.sysBusinessExtendMapper.insert(sysBusinessExtend1);
        }
        return new ResponseVo(true,"添加成功");
    }

    @Override
    public Map<String, Object> getPdfPrintInfo(String busId) throws  Exception {
        //设置文件存放的路径
        String filePath = "D:/temp/template";
        filePath = filePath.replaceAll("\\\\", "/");
        filePath = filePath.length() == filePath.lastIndexOf("/") ? filePath : filePath + "/";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        Map params = new HashMap();
        //设置数据源信息
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:config/spring-context.xml");
        DataSource dataSource =(DruidDataSource)applicationContext.getBean("dataSource");
        if(dataSource!=null){
           parameterMap.put("datasource",dataSource);
        }else{
            JREmptyDataSource emptyDataSource = new JREmptyDataSource();
             parameterMap.put("datasource",emptyDataSource);
        }
        //添加水印信息
        params.put("isShowImage", "true");
        params.put("imageContext", new FileInputStream(filePath + "商委水印.jpg"));
        parameterMap.putAll(params);
        parameterMap.put("format","pdf");
        parameterMap.put("name","demo1");
        parameterMap.put("busId",Integer.parseInt(busId));
        parameterMap.put("url", filePath+"demo1.jrxml");
        return parameterMap;
    }

    @Override
    public DataGridRespVo queryBusinessList(int page, int rows) {
        List<SysBusiness> list = sysBusinessMapper.selectAll();
        PageHelper.startPage(page,rows);
        PageInfo<SysBusiness> pageInfo = new PageInfo<>(list);
        DataGridRespVo dataGridRespVo = new DataGridRespVo();
        dataGridRespVo.setRows(list);
        dataGridRespVo.setTotal(pageInfo.getTotal());
        return dataGridRespVo;
    }
}
